#include <windows.h>
#include "resource.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// LAST modified 9.08 pm Vineri
const char g_szClassName[] = "myWindowClass";
HINSTANCE hInst;
HDC hDC;
PAINTSTRUCT Ps;
int deseneaza;
int deseneaza2;
int contor=0;
int ok;

#define H 10

//definesc o structura care sa stie exact cate dreptunghiuri am introdus in agenda si pozitia acestuia de fix 

struct coord
{
	int x;
	int y;
	int contor;

};

coord C[10];

struct ceamintrodus
{
	int x;
	int y;
	int x1;
	int y1;
	char Nume[15];
	char Prenume[15];
	char Nrtel[10];
	int OK;

};

ceamintrodus Introdus[40];
 
  // ma ajuta la desenare
struct tel 
{
	char *nrtel;
	tel *next;
};
struct nod
{
	char *Nume;
	char *Prenume;
	tel *head;
	nod *next;
};
struct HT
{
	nod *head;
};

int k; // folosit la Hash RETURN

HT M[10];
HWND h_d2=NULL;

char nume[20],prenume[20],nrtel[20];
char NumePrenume[40];
int dimsir,dimtel;

void init_hash()
{
	int i;
	for(i=0;i<H;i++)
		M[i].head=NULL;
	
	for( i=0;i<10;i++)
	{
		C[i].x=150;
		C[i].y=150 + i*70;
		C[i].contor=0;
	}

	for( i=0;i<40;i++)
		Introdus[i].OK=0;
}




int hash( char *sir)
{
	int i,sum=0;
	for(i=0;i<strlen(sir);i++)
		sum+=sir[i];
	return sum % 10;
}
nod *add_list(char *nume, char *prenume, char *nrtel)
{
	nod *nou=(nod*)malloc(sizeof(nod));
	nou->Nume = (char*)malloc(strlen(nume)*sizeof(char));
	nou->Prenume = (char*)malloc(strlen(prenume)*sizeof(char));
	nou->head = new tel;
	nou->head->nrtel = ( char *)malloc(strlen(nrtel)*sizeof(char));
	nou->head->next=NULL;
	strcpy(nou->Nume,nume);
	strcpy(nou->Prenume,prenume);
	strcpy(nou->head->nrtel,nrtel);
	nou->next=NULL;
	return nou;
}
void add_contact(char *nume, char *prenume, char *nrtel)
{
	 k=hash(nume);
	 // C[k].y ramane acelasi;

	nod *poz= M[k].head;
	if ( poz == NULL)
	{
		nod *crt=add_list(nume,prenume,nrtel);
		M[k].head=crt;
		return;
	}
	else
	{
		nod *crt=add_list(nume,prenume,nrtel);
		crt->next=M[k].head;
		M[k].head=crt;
		return;
	}
}
void free_hash ()
{
	nod *crt,*aux;
	tel *crt1,*aux1;
	int i;
	for (i=0;i<H;i++)
	{
		if (M[i].head!=NULL)
		{
			crt=M[i].head;
			while (crt)
			{
				crt1=crt->head;
				while (crt1)
				{
					aux1=crt1;
					crt1=crt1->next;
					free(aux1);
				}
				aux=crt;
				crt=crt->next;
				free (aux);
			}
		}
	}
}


nod *find_contact(char *nume, char *prenume)
{
	int k=hash(nume);
	nod *crt = M[k].head;
	if( crt == NULL)
	{
		MessageBox (NULL,"Nu s-a putut gasi persoana","EROARE",MB_OK | MB_ICONERROR);
		return NULL;
	}
	while ( crt != NULL)
	{
		if( strcmp(crt->Nume,nume) == 0 && strcmp(crt->Prenume,prenume) == 0 )
		{
			return crt;
		}
		crt=crt->next;
	}
}
void show_contact (HWND hwnd,char *nume, char *prenume)
{
	SendDlgItemMessage(hwnd, IDC_LIST1, LB_RESETCONTENT, 0, 0);
	nod *crt=find_contact(nume,prenume);
	if (crt==NULL)
	{
	}
	else
	{
	tel *nr=NULL;
	nr=crt->head;
	char buffer[1000];
	strcpy (buffer,crt->Nume);
	strcat (buffer," ");
	strcat (buffer,crt->Prenume);
	while (nr)
	{
		strcat (buffer," ");
		strcat (buffer,nr->nrtel);
		nr=nr->next;
	}
	SendDlgItemMessage(hwnd, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)buffer);
	}
}
int add_tel(char *nume, char *prenume, char *nrtel)
{
	int k=hash(nume);
	nod *gasit=find_contact(nume,prenume);
	if( gasit != NULL)
	{
		tel *aux=gasit->head;
		while(aux->next != NULL)
			aux=aux->next;
		tel *nou=new tel;
		nou->nrtel = (char *) malloc(strlen(nrtel)*sizeof(char));
		strcpy(nou->nrtel,nrtel);
		nou->next=NULL;
		aux->next=nou;
		return 1;
	}
	else
		return 0;
}
int erase_contact(char *nume, char *prenume)
{
	int k=hash(nume);
	nod *gasit=find_contact(nume,prenume);
	if (gasit==NULL)
		return 0;
	else
	{
	if( M[k].head->next ==NULL && gasit ->next == NULL)
	{
		nod *aux=gasit;
		gasit=gasit->next;
		M[k].head=NULL;
		free(aux);
		MessageBox(NULL,"Stergerea s-a efectuat cu succes","Stergere",MB_OK | MB_ICONINFORMATION);
		return 1;
	}
	else
	{
		nod *crt=M[k].head;
		int nr=0;
		if( crt == gasit )
		{
			//capul listei
			M[k].head=crt->next;
			free(crt);
			MessageBox(NULL,"Stergerea s-a efectuat cu succes","Stergere",MB_OK | MB_ICONINFORMATION);
			return 1;
		}
		while(crt->next != gasit)
			crt=crt->next;
		//retin de pointerul anterior
		crt->next=gasit->next;
		free(gasit);
		MessageBox(NULL,"Stergerea s-a efectuat cu succes","Stergere",MB_OK | MB_ICONINFORMATION);
		return 1;
		
	}
	}
}
void show_hash(HWND hwnd)
{
	SendDlgItemMessage(hwnd, IDC_LIST1, LB_RESETCONTENT, 0, 0);
	int i;
	char buffer[1000];
	for (i=0;i<H;i++)
	{
		nod *crt=M[i].head;	
		if(crt!=NULL)
		{
			while(crt!=NULL)
			{
				strcpy (buffer,crt->Nume);
				strcat (buffer," ");
				strcat (buffer,crt->Prenume);
				tel *crt1=crt->head;
				while( crt1 != NULL)
				{
					strcat (buffer," ");
					strcat (buffer,crt1->nrtel);
					strcat (buffer,"\n");
					crt1=crt1->next;
				}
				SendDlgItemMessage(hwnd, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)buffer);
				crt=crt->next;
			}
		}

	}
	
}

void insert_contact (HWND hwnd)
{
	int len1,len2,len3;
	char *buffer1,*buffer2,*buffer3;
	len1 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITNUME));
	if (len1>=0) 
	{
		buffer1=(char*)malloc(sizeof(char)*(len1+1));
		GetDlgItemText(hwnd, IDC_EDITNUME, buffer1, len1+1);
	}
	len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITPRENUME));
	if (len2>=0)
	{
		buffer2=(char*)malloc(sizeof(char)*(len2+1));
		GetDlgItemText(hwnd, IDC_EDITPRENUME, buffer2, len2+1);
	}
	len3 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITNUMAR));
	if (len3>=0)
	{
		buffer3=(char*)malloc(sizeof(char)*(len3+1));
		GetDlgItemText(hwnd, IDC_EDITNUMAR, buffer3, len3+1);
	}
	strcpy(nume,buffer1);
	strcpy(prenume,buffer2);
	strcpy(nrtel,buffer3);
	strcpy(NumePrenume,"");
	strcat(NumePrenume,nume);
	strcat(NumePrenume," ");
	strcat(NumePrenume,prenume);
	add_contact(buffer1,buffer2,buffer3);
	free (buffer1); free(buffer2); free(buffer3);
}
int insert_number (HWND hwnd)
{
	int len1,len2,len3,i;
	char *buffer1,*buffer2,*buffer3;
	len1=GetWindowTextLength(GetDlgItem(hwnd,IDC_EDITNUME));
	if (len1>0)
	{
		buffer1=(char*)malloc(sizeof(char)*(len1+1));
		GetDlgItemText(hwnd, IDC_EDITNUME, buffer1, len1+1);
	}
	len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITPRENUME));
	if (len2>=0)
	{
		buffer2=(char*)malloc(sizeof(char)*(len2+1));
		GetDlgItemText(hwnd, IDC_EDITPRENUME, buffer2, len2+1);
	}
	len3 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITNUMAR));
	if (len3>=0)
	{
		buffer3=(char*)malloc(sizeof(char)*(len3+1));
		GetDlgItemText(hwnd, IDC_EDITNUMAR, buffer3, len3+1);
	}
	i=add_tel(buffer1,buffer2,buffer3);
	free (buffer1); free(buffer2); free(buffer3);
	return i;
}
int del_contact (HWND hwnd)
{
	int i;
	int len1,len2;
	char *buffer1,*buffer2;
	len1=GetWindowTextLength(GetDlgItem(hwnd,IDC_EDITNUME));
	if (len1>0)
	{
		buffer1=(char*)malloc(sizeof(char)*(len1+1));
		GetDlgItemText(hwnd, IDC_EDITNUME, buffer1, len1+1);
	}
	len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITPRENUME));
	if (len2>=0)
	{
		buffer2=(char*)malloc(sizeof(char)*(len2+1));
		GetDlgItemText(hwnd, IDC_EDITPRENUME, buffer2, len2+1);
	}
	i=erase_contact(buffer1,buffer2);
	free (buffer1); free(buffer2);
	return i;
}
void show (HWND hwnd)
{
	SendDlgItemMessage(hwnd, IDC_LIST1, LB_RESETCONTENT, 0, 0);
	int len1,len2;
	char *buffer1,*buffer2;
	len1=GetWindowTextLength(GetDlgItem(hwnd,IDC_EDITNUME));
	if (len1>0)
	{
		buffer1=(char*)malloc(sizeof(char)*(len1+1));
		GetDlgItemText(hwnd, IDC_EDITNUME, buffer1, len1+1);
	}
	len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITPRENUME));
	if (len2>=0)
	{
		buffer2=(char*)malloc(sizeof(char)*(len2+1));
		GetDlgItemText(hwnd, IDC_EDITPRENUME, buffer2, len2+1);
	}
	show_contact(hwnd,buffer1,buffer2);
	free(buffer1);free(buffer2);
}



BOOL CALLBACK dialog1 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	
	
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDADAUGA:
			insert_contact(hwnd);
			deseneaza2=2;
			C[k].contor++; //contorul ptr a determina pe fiecare linie, pe a cata pozitie sa introduc
			contor++;		//contorul ptr structura Introdus[40];
			MessageBox(NULL,"Inserarea s-a realizat cu succes !","Inserare Dialog",MB_OK | MB_ICONINFORMATION);
			SendMessage(hwnd,WM_PAINT,(WPARAM)hDC,0);
			UpdateWindow(hwnd);
			EndDialog(hwnd,0);
			break;
		case IDRENUNTA:
			EndDialog(hwnd,0);
			break;
		}
		break;
		case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
		
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog2 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int i;
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			i=insert_number(hwnd);
			EndDialog(hwnd,0);
			if (i==1)
			MessageBox(hwnd,"Numarul s-a inserat cu succes","Adauga",MB_OK | MB_ICONINFORMATION);
			break;
		case IDCANCEL:
			EndDialog(hwnd,0);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog3 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int i;
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDSTERGE:
			 i=del_contact(hwnd);
			 EndDialog(hwnd,0);
			 if (i==1)
			 {
				MessageBox(NULL,"Stergerea s-a efectuat cu succes","Stergere",MB_OK | MB_ICONINFORMATION);
			 }
			 else
			 {
				 MessageBox (NULL,"Nu s-a putut gasi persoana","EROARE",MB_OK | MB_ICONERROR);
			 }
			 break;
		case IDRENUNTA:
			EndDialog(hwnd,0);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog4 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		show_hash(hwnd);
		return TRUE;
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog5 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_CAUTA:
			show(hwnd);
			break;
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog6 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog8 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog9 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog10 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog11 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog12 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return TRUE;
		break;
	case WM_COMMAND:
		switch ( LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);
			break;
		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
BOOL CALLBACK dialog7(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HWND d8=NULL,d9=NULL,d10=NULL,d11=NULL,d12=NULL;
	HDC hDC, MemDCExercising;
    PAINTSTRUCT Ps;
    HBITMAP bmpExercising;

	switch(msg)
	{
	case WM_INITDIALOG :
		return TRUE;
		break;
	case WM_COMMAND:
		switch( LOWORD (wParam))
		{
		case IDOK:
			EndDialog(hwnd,0);

			break;
		case IDC_RADIO1:
			d8=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG8),hwnd,dialog8);
			if (d8 != NULL)
			{
				ShowWindow(d8,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 8","EROARE",MB_OK | MB_ICONERROR);
			}
			break;
		case IDC_RADIO2:
			d9=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG9),hwnd,dialog9);
			if (d9 != NULL)
			{
				ShowWindow(d9,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 9","EROARE",MB_OK | MB_ICONERROR);
			}
			break;
		case IDC_RADIO3:
			d10=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG10),hwnd,dialog10);
			if (d10 != NULL)
			{
				ShowWindow(d10,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 8","EROARE",MB_OK | MB_ICONERROR);
			}
			break;

		case IDC_RADIO4:
			d11=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG11),hwnd,dialog11);
			if (d11 != NULL)
			{
				ShowWindow(d11,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 11","EROARE",MB_OK | MB_ICONERROR);
			}
			break;

		case IDC_RADIO5:
			d12=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG8),hwnd,dialog8);
			if (d12 != NULL)
			{
				ShowWindow(d12,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 12","EROARE",MB_OK | MB_ICONERROR);
			}
			break;

		}
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default :
		return FALSE;
	}
	return TRUE;
}

void functie_desenare_nou()
{
	Rectangle(hDC,60,100,150,150);
	TextOut(hDC,75,110," 0 ",3);
	Rectangle(hDC,60,170,150,220);
	TextOut(hDC,75,180," 1 ",3);
	Rectangle(hDC,60,240,150,290);
	TextOut(hDC,75,250," 2 ",3);
	Rectangle(hDC,60,310,150,360);
	TextOut(hDC,75,320," 3 ",3);
	Rectangle(hDC,60,370,150,430);
	TextOut(hDC,75,380," 4 ",3);
	Rectangle(hDC,60,440,150,500);
	TextOut(hDC,75,450," 5 ",3);
	Rectangle(hDC,60,510,150,570);
	TextOut(hDC,75,520," 6 ",3);
	Rectangle(hDC,60,580,150,640);
	TextOut(hDC,75,590," 7 ",3);
	Rectangle(hDC,60,650,150,710);
	TextOut(hDC,75, 660," 8 ",3);
	Rectangle(hDC,60,720,150,780);
	TextOut(hDC,75,730," 9 ",3);
	return;
}


void functie_desenare(HWND hwnd,int ok)
{

	int val=((C[k].contor) * 240);
	C[k].x = 150 + val;		

	//Rectangle(hDC,C[k].x-240,C[k].y-25 ,C[k].x-190 ,C[k].y-24);
	//trag dreptunghi
	
	strcpy(Introdus[contor].Nume,nume);
	strcpy(Introdus[contor].Prenume,prenume);
	strcpy(Introdus[contor].Nrtel,nrtel);

	Introdus[contor].x=C[k].x-190;
	Introdus[contor].y=C[k].y-50;
	Introdus[contor].x1=C[k].x;
	Introdus[contor].y1=C[k].y;
	Introdus[contor].OK=1;
	
	//afisez informatii text
	dimsir=strlen(NumePrenume);
	dimtel=strlen(nrtel);

	//Rectangle(hDC,C[k].x-190,C[k].y-50,C[k].x,C[k].y);
	Rectangle(hDC,Introdus[contor].x,Introdus[contor].y,Introdus[contor].x1,Introdus[contor].y1);
	TextOut(hDC,Introdus[contor].x + 3,Introdus[contor].y1-48,NumePrenume,dimsir);
	TextOut(hDC,Introdus[contor].x1 - 187,Introdus[contor].y1-31,nrtel,dimtel);
	
	for(int nr=0;nr<40;nr++)
	{
		if(Introdus[nr].OK )
			{
				Rectangle(hDC,Introdus[nr].x,Introdus[nr].y,Introdus[nr].x1,Introdus[nr].y1);
				int dimtel=strlen(Introdus[nr].Nrtel);
				strcpy(NumePrenume,Introdus[nr].Nume);
				strcat(NumePrenume," ");
				strcat(NumePrenume,Introdus[nr].Prenume);
				dimsir=strlen(NumePrenume);

				TextOut(hDC,Introdus[nr].x +3,Introdus[nr].y1-48,NumePrenume,dimsir);
				TextOut(hDC,Introdus[nr].x1-187,Introdus[nr].y1-31,Introdus[nr].Nrtel,dimtel);
			}
		
	}
}

void insert_fisier (char *filename)
{
	FILE *in;
	in=fopen(filename,"r");
	if (in==NULL)
	{
		MessageBox (NULL,"Eroare la deschiderea fisierului","ERROR",MB_OK|MB_ICONERROR);
		return;
	}
	char *buffer1,*buffer2,*buffer3,buffer[1000],*p;
	buffer1=(char *)malloc(sizeof(char)*100);
	buffer1=NULL;
	buffer2=(char *)malloc(sizeof(char)*100);
	buffer2=NULL;
	buffer3=(char *)malloc(sizeof(char)*100);
	buffer3=NULL;
	while (!feof(in))
	{
		fgets(buffer,1000,in);
		buffer1=strtok(buffer," ");
		buffer2=strtok(NULL," ");
		buffer3=strtok(NULL," \n");
		if ((buffer1!=NULL) && (buffer2!=NULL) && (buffer3!=NULL))
		add_contact(buffer1,buffer2,buffer3);
		while (buffer3)
		{
			buffer3=strtok(NULL," \n");
			if (buffer3!=NULL)
			add_tel(buffer1,buffer2,buffer3);
		}
	}
	fclose (in);
}
void salvare_fisier (char *filename)
{
	FILE *out;
	out=fopen (filename,"w");
	if (out==NULL)
	{
		MessageBox (NULL,"Eroare la deschiderea fisierului","ERROR",MB_OK|MB_ICONERROR);
		return;
	}
	int i;
	char buffer[1000];
	for (i=0;i<H;i++)
	{
		nod *crt=M[i].head;	
		if(crt!=NULL)
		{
			while(crt!=NULL)
			{
				strcpy (buffer,crt->Nume);
				strcat (buffer," ");
				strcat (buffer,crt->Prenume);
				tel *crt1=crt->head;
				while( crt1 != NULL)
				{
					strcat (buffer," ");
					strcat (buffer,crt1->nrtel);
					//strcat (buffer,"\n");
					crt1=crt1->next;
				}
				fprintf (out,"%s\n",buffer);
				crt=crt->next;
			}
		}
	}
	fclose (out);
}
// Step 4: the Window Procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";
	HWND d1=NULL,d2=NULL,d3=NULL,d4=NULL,d5=NULL,d6=NULL,d7=NULL;
	
	switch(msg)
    {
	case WM_CREATE:
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{


		case ID_FISIER_NOU:
			if (MessageBox (hwnd,"Incarcati datele din fisier?","Nou",MB_YESNO | MB_ICONINFORMATION)==IDNO)
			init_hash();
			else
			{
				ZeroMemory(&ofn, sizeof(ofn));
				ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
				ofn.hwndOwner = hwnd;
				ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
				ofn.lpstrFile = szFileName;
				ofn.nMaxFile = MAX_PATH;
				ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
				ofn.lpstrDefExt = "txt";
				if(GetOpenFileName(&ofn))
				{
					insert_fisier(szFileName);
				}
			}
			deseneaza=1;
			MessageBox (hwnd,"Agenda s-a initializat cu succes","NOU",MB_OK | MB_ICONINFORMATION);
			UpdateWindow(hwnd);
			SendMessage(hwnd,WM_PAINT,(WPARAM)hDC,0);
			
			break;

		case ID_FISIER_INSERARECONTACT:
			d1=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG1),hwnd,dialog1);
			if (d1!=NULL)
			{
				ShowWindow(d1,SW_SHOW);
				
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 1","EROARE",MB_OK | MB_ICONERROR);
			}
			UpdateWindow(hwnd);
			break;

		case ID_FISIER_INSERARENUMAR:
			d2=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG2),hwnd,dialog2);
			if (d2!=NULL)
			{
				ShowWindow(d2,SW_SHOW);
			}
			else
			{
				MessageBox(hwnd,"Eroare la crearea dialogului 2","EROARE",MB_OK | MB_ICONERROR);
			}	
			break;
		case ID_FISIER_STERGERE:
			d3=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG3),hwnd,dialog3);
			if (d3!=NULL)
			{
				ShowWindow(d3,SW_SHOW);
			}
			break;
		case ID_FISIER_IESIRE:
			if (MessageBox(hwnd,"Sunteti sigur?","Iesire", MB_YESNO | MB_ICONINFORMATION)==IDYES)
			{
				free_hash();
				PostQuitMessage(0);
			}
			break;
		case ID_AFISARE_AFISAREAGENDA:
			d4=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG4),hwnd,dialog4);
			if (d4!=NULL)
			{
				ShowWindow(d4,SW_SHOW);
			}
			break;
		case ID_AFISARE_AFISARECONTACT:
			d5=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG5),hwnd,dialog5);
			if (d5!=NULL)
			{
				ShowWindow(d5,SW_SHOW);
			}
			break;
		case ID_AFISARE_SALVARE1:
			ZeroMemory(&ofn, sizeof(ofn));
				ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
				ofn.hwndOwner = hwnd;
				ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
				ofn.lpstrFile = szFileName;
				ofn.nMaxFile = MAX_PATH;
				ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
				ofn.lpstrDefExt = "txt";
				if(GetSaveFileName(&ofn))
				{
					salvare_fisier(szFileName);
				}
				break;
		case ID_MAIMULTE_DESPRE40011 :
			d6=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG6),hwnd,dialog6);
			if (d6 != NULL)
			{
				ShowWindow(d6,SW_SHOW);
			}
			break;
		case ID_MAIMULTE_DESPRE1:
			d7=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG7),hwnd,dialog7);
			if ( d7 != NULL)
			{
				ShowWindow (d7,SW_SHOW);
			}
			break;

	
		}
		break;
	case WM_CLOSE:
		free_hash();
		DestroyWindow(hwnd);
		break;
    case WM_DESTROY:
		free_hash();
		PostQuitMessage(0);
        break;
	
		case WM_PAINT:
        {
                InvalidateRect (hwnd, NULL, TRUE);
                UpdateWindow (hwnd);
				ShowWindow(hwnd,SW_SHOW);

                hDC=BeginPaint(hwnd,&Ps);
                
                SetTextColor(hDC,RGB(0,0,128));
                TextOut(hDC,612,15,"Agenda Telefonica",17);

				
				if(deseneaza==1) 
				{
					UpdateWindow (hwnd);
					functie_desenare_nou();
					
					deseneaza=1;
					
				}
				
				if(deseneaza2 == 2)
				{
					UpdateWindow (hwnd);
					ok=1;
					functie_desenare(hwnd,ok);
					
					deseneaza2=2;
				}
				UpdateWindow(hwnd);
                EndPaint(hwnd,&Ps);
        }
        break;
    default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Agenda telefonica - Tabele de dispersie",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, SW_MAXIMIZE);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}